(function () {
    // 防止重复创建抽屉
    if (document.getElementById("chrome-extension-drawer")) return;

    // 创建抽屉容器
    const drawer = document.createElement("div");
    drawer.id = "chrome-extension-drawer";
    drawer.style.cssText = `
    position: fixed;
    top: 0;
    right: -320px; /* 隐藏在右侧 */
    width: 320px;
    height: 100%;
    background-color: #fff;
    box-shadow: -2px 0 10px rgba(0,0,0,0.2);
    transition: right 0.3s ease;
    z-index: 999999;
    padding: 16px;
    box-sizing: border-box;
  `;

    // 抽屉内部内容（关闭按钮 + 操作按钮）
    drawer.innerHTML = `
    <div style="text-align: right; margin-bottom: 16px;">
      <button id="btn-close-drawer" style="
        padding: 6px 10px;
        background: #f44336;
        color: white;
        border: none;
        border-radius: 4px;
        cursor: pointer;">关闭</button>
    </div>
    <div id="drawer-buttons"></div>
  `;

    document.body.appendChild(drawer);

    // 插入打开按钮（控制抽屉打开）
    const openDrawerBtn = document.createElement("button");
    openDrawerBtn.textContent = "ollama抽屉";
    openDrawerBtn.style.cssText = `
    position: fixed;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
    background: #1976d2;
    color: white;
    border: none;
    border-radius: 4px 0 0 4px;
    padding: 10px;
    cursor: pointer;
    z-index: 999998;
  `;
    document.body.appendChild(openDrawerBtn);
    let clickTimer = null;

    openDrawerBtn.addEventListener("click", () => {
        // 延迟执行单击操作
        if (clickTimer == null) {
            clickTimer = setTimeout(() => {
                // 单击的操作
                drawer.style.right = "0";
            }, 250); // 250ms内没有第二次点击就执行
        }
    });


    drawer.querySelector("#btn-close-drawer").addEventListener("click", () => {
        drawer.style.right = "-320px";
    });

    // 抽屉中的按钮区域
    const drawerButtons = drawer.querySelector("#drawer-buttons");

    // 防止重复插入按钮
    if (!document.getElementById("btn-open-popup")) {
        // 创建“打开弹窗”按钮
        const btnPopup = document.createElement("button");
        btnPopup.id = "btn-open-popup";
        btnPopup.textContent = "请求页面";
        btnPopup.style.cssText = `
      width: 100%;
      margin-bottom: 12px;
      padding: 10px;
      background: #1976d2;
      color: white;
      border: none;
      border-radius: 6px;
      cursor: pointer;
    `;

        // 创建“打开设置”按钮
        const btnSetting = document.createElement("button");
        btnSetting.id = "btn-open-setting";
        btnSetting.textContent = "设置server";
        btnSetting.style.cssText = `
      width: 100%;
      padding: 10px;
      background: #4caf50;
      color: white;
      border: none;
      border-radius: 6px;
      cursor: pointer;
    `;

        // 创建 iframe 函数
        function createIframe(url, id) {
            if (document.getElementById("content-frame")) return;
            const iframe = document.createElement("iframe");
            iframe.id = id;
            iframe.src = chrome.runtime.getURL(url);
            iframe.style.cssText = `
            position: fixed;
            bottom: 140px;
            right: 20px;
            width: 600px;
            height: 480px;
            z-index: 10000;
            border: none;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.3);
            background: white;
          `;
            document.body.appendChild(iframe);
            drawer.style.right = "-320px";
        }

        btnPopup.onclick = () => createIframe("html/popup.html", "popup-frame");
        openDrawerBtn.addEventListener("dblclick", () => {
            // 双击事件触发时，取消单击的定时器
            if (clickTimer) {
                clearTimeout(clickTimer);
                clickTimer = null;
            }
            createIframe("html/popup.html", "popup-frame");
        });
        btnSetting.onclick = () => createIframe("html/setting.html", "setting-frame");

        // 插入按钮到抽屉中
        drawerButtons.appendChild(btnPopup);
        drawerButtons.appendChild(btnSetting);
    }
})();


// 监听来自 iframe 的消息
window.addEventListener('message', (event) => {
    if (event.data === 'close_popup_frame') {
        const iframe = document.getElementById('popup-frame');
        if (iframe) iframe.remove();
    }
    if (event.data === 'close_setting_frame') {
        const iframe = document.getElementById('setting-frame');
        if (iframe) iframe.remove();
    }
});
